<?php
/**
 * Created by PhpStorm.
 * User: qiyi
 * Date: 2018/5/24
 * Time: 18:29
 */

namespace Common\helper;


class FileLock
{
    /**
     * 阻塞文文件锁(等待)
     */
    public static function blockLock(){
        $fp = fopen("lock.txt", "r");
        if(flock($fp,LOCK_EX))
        {
            //..处理订单
            flock($fp,LOCK_UN);
        }
        fclose($fp);
    }

    /**
     * 非阻塞模式文件锁
     */
    public static function  NoBlockLock(){
        $fp = fopen("lock.txt", "r");
        if(flock($fp,LOCK_EX | LOCK_NB))
        {
            //..处理订单
            flock($fp,LOCK_UN);
        }
        else
        {
            echo "系统繁忙，请稍后再试";
        }
        fclose($fp);
    }

}